# -*- encoding: utf-8 -*- 
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest, time

class BaiduSpace(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://hi.baidu.com"
        self.verificationErrors = []
    
    def test_baiduSpace(self):
        driver = self.driver
        driver.maximize_window()
        driver.get(self.base_url + "/go/login")
        driver.find_element_by_id("username").click()
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("zhengchunxi@foxmail.com")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("testMiaozhen")
        time.sleep(1)
        driver.find_element_by_css_selector("div.mod-guide-form-input.mod-guide-form-input-12").click()
        time.sleep(1)
        driver.find_element_by_id("memPass").click()
        time.sleep(1)
        driver.find_element_by_xpath("//form[@id='formLogin']/div/div/div/div[3]/div[5]/div[3]/a/span[2]").click()
        driver.find_element_by_css_selector("div.ent-tip").click()
        time.sleep(1)
        driver.find_element_by_id("title").click()
        driver.find_element_by_id("title").clear()
        driver.find_element_by_id("title").send_keys("PythonTest")
        time.sleep(1)
#        driver.find_element_by_css_selector("body").click()
        driver.find_element_by_css_selector("#edui31_body > div.edui-box.edui-icon").click()
        driver.find_element_by_css_selector("#edui29_body > div.edui-box.edui-label").click()
        time.sleep(1) 
        
        driver.switch_to_frame("edui27_iframe")
        time.sleep(10)         
        driver.find_elements_by_xpath("//div[@id='picTab']/span[3]/a").click()
        time.sleep(3)
        picture = 'http://postimg1.mop.com/2011/04/05/13020007455863507.jpg'
        driver.find_elements_by_xpath("//div[@id='picNet']/div[1]/span[a]").send_keys(picture)
        driver.find_elements_by_xpath("//div[@id='edui27_buttons']/div[1]/div/div/div/div[2]").click()
        
        time.sleep(3)
        driver.find_element_by_css_selector("body").click()
        driver.find_element_by_id("qPubBtnSubmit").click()
        driver.find_element_by_id("qNavRightExit").click()
        driver.find_element_by_id("qNavRightExit").click()
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException: return False
        return True
    
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
